package cn.wywk.netty.push.example.service.impl;

import cn.wywk.netty.push.example.config.NettyConfig;
import cn.wywk.netty.push.example.service.PushService;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @description: 推送消息处理类
 * @projectName:netty-push-example
 * @see:cn.wywk.netty.push.example.service.impl
 * @author:zhangwenxue
 * @createTime:2021/3/2 17:38
 * @version:1.0
 */
@Service
public class PushServiceImpl implements PushService {

    private Logger logger = LoggerFactory.getLogger(PushServiceImpl.class);

    @Override
    public void pushMsgToOne(String userId, String msg) {
        Map<String, Channel> userChannelMap = NettyConfig.getUserChannelMap();
        Channel channel = userChannelMap.get(userId);
        channel.writeAndFlush(new TextWebSocketFrame(msg));
    }

    @Override
    public void pushMsgToAll(String msg) {
        NettyConfig.getChannelGroup().writeAndFlush(new TextWebSocketFrame(msg));
    }
}
